<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use \think\Request;
use think\facade\View;
use app\common\model\Advert as AdvertModel;

class Advert extends Base
{
    /*
     *轮播图列表
     */
    public function index(Request $request)
    {
        $AdvertModel = new AdvertModel;
        $whe = [];
        $field = '*';
        $order = 'type asc,id desc';
        $data = $AdvertModel->getList($whe, $field, $order, $this->page);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        View::assign('data', $data['list']);
        return View::fetch('Advert/index');
    }

    /*
     * 轮播图添加
     */
    public function add(Request $request)
    {
        if (!$request->isPost()) {
            return View::fetch('Advert/add');
        } else {
            $data = input('post.');
            if ($data['type'] != 1 && $data['type'] != 3) {
                return $this->renderError("类型选择错误");
            }
            if ($data['ttype'] == 1){
                if (!$data['coupon_id'] || $data['coupon_id'] < 0){
                    return $this->renderError("优惠券id错误");
                }
                $is_find = CouponReceivemodel::where('id',$data['coupon_id'])->find();
                if(!$is_find){
                    return $this->renderError("优惠券不存在");
                }
            }
            if ($data['ttype'] == 2 || $data['ttype'] == 3){
                if (!$data['goods_id'] || $data['goods_id'] < 0){
                    return $this->renderError("盒子id错误");
                }
                $is_find = \app\common\model\Goods::where('id',$data['goods_id'])->find();
                if(!$is_find){
                    return $this->renderError("盒子不存在");
                }
            }
            if (RegZero($data['sort'])) {
                return $this->renderError("排序值请输入整数");
            }
            if (empty($data['imgurl'])) {
                return $this->renderError("请上传图片");
            }
            $data['addtime'] = time();
            $dd = AdvertModel::insert($data);
            if ($dd) {
                return $this->renderSuccess("轮播图添加成功");
            } else {
                return $this->renderError("轮播图添加失败");
            }
        }
    }

    /*
     * 轮播图修改
     */
    public function edit(Request $request)
    {
        $AdvertModel = new AdvertModel;
        if (!$request->isPost()) {
            $id = $request->param('id');
            $data = AdvertModel::where(['id' => $id])->find();
            if (!$data) {
                return $this->renderError("请求参数错1");
            }
            View::assign('data', $data);
            return View::fetch('Advert/edit');
        } else {
            $data = input('post.');
            if (empty($data['id'])) {
                return $this->renderError("请求参数错误");
            }
            $info = AdvertModel::where(['id' => $data['id']])->find();
            if (!$info) {
                return $this->renderError("请求参数错1");
            }
            if ($data['ttype'] == 1){
                if (!$data['coupon_id'] || $data['coupon_id'] < 0){
                    return $this->renderError("优惠券id错误");
                }
                $is_find = \app\common\model\Coupon::where('id',$data['coupon_id'])->find();
                if(!$is_find){
                    return $this->renderError("优惠券不存在");
                }
            }
            if ($data['ttype'] == 2 || $data['ttype'] == 3){
                if (!$data['goods_id'] || $data['goods_id'] < 0){
                    return $this->renderError("盒子id错误");
                }
                $is_find = \app\common\model\Goods::where('id',$data['goods_id'])->find();
                if(!$is_find){
                    return $this->renderError("盒子不存在");
                }
            }
            if (RegZero($data['sort'])) {
                return $this->renderError("排序值请输入整数");
            }
            if (empty($data['imgurl'])) {
                return $this->renderError("请上传图片");
            }
            $data['update_time'] = time();
            $dd = $info->save($data);
            if ($dd) {
                return $this->renderSuccess("轮播图编辑成功");
            } else {
                return $this->renderError("轮播图编辑失败");
            }
        }
    }

    /*
     * 轮播图 删除
     */
    public function del(Request $request)
    {
        $id = $request->param('id');
        $info = AdvertModel::where(['id' => $id])->find();
        if (!$info) {
            return $this->renderError("请求参数错1");
        }
        $dd = AdvertModel::where('id', '=', $id)->delete();
        if ($dd) {
            return $this->renderSuccess("轮播图删除成功");
        } else {
            return $this->renderError("轮播图删除失败");
        }
    }
}